package cn.jilian.trans.bizz.util;

import javax.servlet.http.HttpSession;

import cn.jilian.entity.User;

import java.util.Objects;

public class SessionUtil {
	
	private static final String IMAGE_CODE="authCode";
	
	public static final String USER_INFO="userInfo";
	
	public static boolean checkLogin(HttpSession session){
		return session.getAttribute(USER_INFO)!=null;
	}
	public static void cacheUserInfo(HttpSession session,User userInfo){
		session.setAttribute(USER_INFO, userInfo);
	}
	
	 public static void cacheImageAuthCode(HttpSession session,String code) {
	    	session.setAttribute(IMAGE_CODE, code);
	    }
	
	public static void clearImageCode(HttpSession session){
		session.setAttribute(IMAGE_CODE, null);
	}
	
//	public static Long getUserMetaId(HttpSession session) {
//		User user=(User) session.getAttribute(USER_INFO);
//		if(user==null)return null;
//		return user.getMeta();
//	}
	
	public static Long getUnitId(HttpSession session) {
		User user=(User) session.getAttribute(USER_INFO);
		if(user==null)return null;
		return user.getUnitId();
	}

    public static User getUserInfo(HttpSession session) {
        Object v = session.getAttribute(USER_INFO);
        if (v == null) return null;
        return (User) v;
    }
    


    public static String getAuthCode(HttpSession session) {
        return Objects.toString(session.getAttribute(IMAGE_CODE), "");
    }
}
